import 'dart:convert';

import 'package:creatures_world/net/taxon_service.dart';
import 'package:creatures_world/pages/base/base_controller.dart';
import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';

class HomeController extends BaseController {

  var animalsTypes = [].obs;
  var speciesCurrentPage = 1;
  var totalAnimalsTypes = 0.obs;
  var showGridOrlist = true.obs;
  var iconic_taxa="";

  var spKinds = [

    {"name":"All","Animalia": 21750,"checked":true,"kind":"","icon":"icon_sp_all.png"},
    {"name":"Animalia","Animalia": 21750,"checked":false,"kind":"animalia","icon":"icon_sp_1.png"},//动物界
    {"name":"Plantae","Plantae": 173483,"checked":false,"kind":"plantae","icon":"icon_sp_2.png"},//植物界
    {"name":"Mammalia","Mammalia": 4481,"checked":false,"kind":"mammalia","icon":"icon_sp_3.png"},//哺乳动物
    {"name":"Aves","Aves": 11069,"checked":false,"kind":"Aves","icon":"icon_sp_4.png"},//鸟类
    {"name":"Fungi","Fungi": 24927,"checked":false,"kind":"fungi","icon":"icon_sp_5.png"},//真菌
    {"name":"Protozoa","Protozoa": 969,"checked":false,"kind":"protozoa","icon":"icon_sp_6.png"},//原生动物
    {"name":"Chromista","Chromista": 3281,"checked":false,"kind":"chromista","icon":"icon_sp_7.png"},//色藻界
    {"name":"Actinopterygii","Actinopterygii": 16073,"checked":false,"kind":"actinopterygii","icon":"icon_sp_fish.png"},//辐鳍鱼纲
    {"name":"Amphibia","Amphibia": 5945,"checked":false,"kind":"amphibia","icon":"icon_sp_9.png"},//两栖纲
    {"name":"Reptilia","Reptilia": 9104,"checked":false,"kind":"reptilia","icon":"icon_sp_8.png"},//爬行纲
    {"name":"Arachnida","Arachnida": 16145,"checked":false,"kind":"arachnida","icon":"icon_sp_11.png"},//蛛形纲
    {"name":"Insecta","Insecta": 211500,"checked":false,"kind":"insecta","icon":"icon_sp_12.png"},//昆虫纲
    {"name":"Mollusca","Mollusca": 19513,"checked":false,"kind":"mollusca","icon":"icon_sp_13.png"},//软体动物门

  ].obs;

  @override
  void onInit() {
    super.onInit();
    loadSpecies(true);
  }

  String getLocale() {
    return TaxonService.lang;
  }

  Future<int> loadSpecies(bool isRefresh) async {
    // String demoUrl=//https://api.inaturalist.org/v1/observations/species_counts?locale=zh-cn&page=1&per_page=30&quality_grade=needs_id,research&order_by=created_at&order=desc
    int addCount = 0;
    if (isRefresh) {
      speciesCurrentPage = 1;
    } else {
      speciesCurrentPage++;
    }

    var pram = <String, dynamic>{
      "page": speciesCurrentPage,
      "per_page": 21,
      "quality_grade": "needs_id,research",
      "order_by": "created_at",
      "order": "desc",
      "iconic_taxa":iconic_taxa,
      "locale": getLocale(),
    };

    var ret = await TaxonService.speciesCounts(pram);
    if (ret.statusCode == 200) {
      var data = jsonDecode(ret.data);

      totalAnimalsTypes.value = data["total_results"];

      int page = data["page"];
      int per_page = data["per_page"];

      List<dynamic> newDatas = data["results"];
      addCount = newDatas.length;

      if (isRefresh) {
        animalsTypes.value = newDatas;
      } else {
        animalsTypes.value.addAll(newDatas);
        animalsTypes.value = List.of(animalsTypes.value);
      }
      debugPrint("得到数据:${newDatas.length}条，总数据:${animalsTypes.length}");
    } else {
      showLyfyMsg(ret.statusMessage, Get.context!);
    }

    return addCount;
  }

  void onSpChecked(dynamic spKind) {
    debugPrint("spKind->  ${spKind}");
    iconic_taxa=spKind["kind"];
    animalsTypes.value=[];
    totalAnimalsTypes.value=0;
    loadSpecies(true);
  }

}
